package Flink.transformation;

import Flink.bean.WaterSensor;
import Flink.function.MyFlatMap;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class FlatmapTrans {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<WaterSensor> stream = env.fromElements(

                new WaterSensor("sensor_1", 1L, 1),
                new WaterSensor("sensor_2", 2L, 2),
                new WaterSensor("sensor_3", 2L, 3),
                new WaterSensor("sensor_4", 3L, 4)

        );
        // 方式一： 传入匿名类实现FilterFunction
        stream.flatMap(new FlatMapFunction<WaterSensor, String>() {
            @Override
            public void flatMap(WaterSensor value, Collector<String> out) throws Exception {
                if (value.getVc() >= 2) {
                    out.collect(value.getId() + " " + value.getVc());
                } else {
                    out.collect(value.getId());
                }
            }
        }).print();
        // 方式二: 传入FilterFunction实现类
        stream.flatMap(new MyFlatMap()).print();

        env.execute();
    }


}
